package com.mobilepay.moneypig.model.activity.mine;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mobilepay.moneypig.R;
import com.mobilepay.moneypig.base.BaseActivity;
import com.mobilepay.moneypig.config.NetConfig;
import com.mobilepay.moneypig.model.application.App;
import com.mobilepay.moneypig.okhttp.HttpUtils;
import com.mobilepay.moneypig.response.ResponseResult;
import com.mobilepay.moneypig.response.home.TeamData;
import com.mobilepay.moneypig.utils.CommonUtil;
import com.mobilepay.moneypig.utils.GsonUtil;
import com.mobilepay.moneypig.utils.acache.ACache;
import com.mobilepay.moneypig.view.DividerLine;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xlm on 2017/7/25 0025
 */

public class MineTeamActivity extends BaseActivity {

    private static final String TAG = "MineTeamActivity";
    private SmartRefreshLayout mRefresh;
    private RecyclerView mRecycleTemam;
    private RelativeLayout mRelativeBack;
    private TeamAdapter teamAdapter;
    private List<TeamData.DataBean> dataBeanList;

    @Override
    protected void onSetContentView() {
        setContentView(R.layout.activity_team);
    }

    @Override
    protected void initViews() {
        mRelativeBack = (RelativeLayout) findViewById(R.id.team_back);
        mRefresh = (SmartRefreshLayout) findViewById(R.id.team_refresh);
        mRecycleTemam = (RecyclerView) findViewById(R.id.team_recycle);
    }

    @Override
    protected void afterOnCreate(Bundle savedInstanceState) {
        setAdapter();
        getTeamData();
    }

    private void setAdapter() {
        dataBeanList = new ArrayList<>();
        teamAdapter = new TeamAdapter(getThis());
        mRecycleTemam.setLayoutManager(new LinearLayoutManager(getThis()));
        teamAdapter.setData(dataBeanList);
        DividerLine dividerLine = new DividerLine(DividerLine.VERTICAL);
        dividerLine.setSize(1);
        dividerLine.setColor(getResources().getColor(R.color.divider_line));
        mRecycleTemam.addItemDecoration(dividerLine);
        mRecycleTemam.setAdapter(teamAdapter);
    }

    @Override
    protected void onSetListener() {
        mRefresh.setEnableLoadmore(false);
        mRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getTeamData();
            }
        });
        mRelativeBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getTeamData() {
        HttpUtils.RequestParam param = new HttpUtils.RequestParam();
        param.add("method", NetConfig.getTeam);
        param.add("uid", String.valueOf(App.getApp().getUserInfo().getData().getUid()));
        param.add("sign", param.getMd5String());
        HttpUtils.getInstance().params(param).executePost(new HttpUtils.HttpListener() {
            @Override
            public void onException(Throwable e) {
                super.onException(e);
                mRefresh.finishRefresh(false);
                if (CommonUtil.isForeground(getThis())) {
                    showToastShort(getString(R.string.net_state));
                    String jsonString = ACache.get(App.getApp()).getAsString(NetConfig.getTeam);
                    if (!CommonUtil.isEmpty(jsonString)) {
                        setTemaData(jsonString);
                    }
                }
            }

            @Override
            public void onHttpSuccess(String string) {
                super.onHttpSuccess(string);
                mRefresh.finishRefresh(true);
                if (HttpUtils.isJson(string)) {
                    ResponseResult responseResult = GsonUtil.GsonToBean(string, ResponseResult.class);
                    if (responseResult.getStatus() == 1) {
                        setTemaData(string);
                        ACache.get(App.getApp()).put(NetConfig.getTeam, string);
                    } else {
                        showToastShort(responseResult.getInfo());
                    }
                }
            }
        });
    }

    private void setTemaData(String string) {
        TeamData teamData = GsonUtil.GsonToBean(string, TeamData.class);
        List<TeamData.DataBean> beanList = teamData.getData();
        if (dataBeanList.size() != 0) {
            dataBeanList.clear();
        }
        dataBeanList.addAll(beanList);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                teamAdapter.notifyDataSetChanged();
            }
        });
    }

    private static class TeamAdapter extends RecyclerView.Adapter<TeamViewHolder> {
        private Context context;
        private LayoutInflater inflater;
        private List<TeamData.DataBean> list;

        TeamAdapter(Context context) {
            this.context = context;
            inflater = LayoutInflater.from(context);
        }

        private void setData(List<TeamData.DataBean> beanList) {
            this.list = beanList;
        }

        @Override
        public TeamViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new TeamViewHolder(inflater.inflate(R.layout.item_team, parent, false));
        }

        @Override
        public void onBindViewHolder(TeamViewHolder holder, int position) {
            TeamData.DataBean dataBean = list.get(position);
            holder.mTextName.setText(CommonUtil.StringOpt(dataBean.getUsername(), 2, 7));
            holder.mTextGrade.setText(String.valueOf(dataBean.getGrade()));
            holder.mTextNum.setText(String.valueOf(dataBean.getCount()));
        }

        @Override
        public int getItemCount() {
            return list.size();
        }
    }

    private static class TeamViewHolder extends RecyclerView.ViewHolder {
        private TextView mTextName, mTextGrade, mTextNum;

        TeamViewHolder(View itemView) {
            super(itemView);
            mTextName = (TextView) itemView.findViewById(R.id.team_name);
            mTextGrade = (TextView) itemView.findViewById(R.id.team_grade);
            mTextNum = (TextView) itemView.findViewById(R.id.team_num);
        }
    }

}
